package com.erp.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * webapi登陆认证
 */
public class WebApiUtils {
//http://47.100.197.8/api/login?redirect_uri=http://47.100.197.8/api/oauth/authorize?client_id=api&response_type=code&scope=read&redirect_uri=http://47.100.197.8/oauth
    public  String get_code(String username,String password){
        String path= PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String method=PropertiesUtils.loadProperties("openapi").getProperty("get_code");
        String url="http://"+path+"/api/login?redirect_uri=http://"+path+"/api/oauth/authorize?client_id=api&response_type=code&scope=read&redirect_uri=http://"+path+"/oauth";
        RequestCode code=new RequestCode();
        code.setPassword(password);
        code.setUrl(url);
        code.setUsername(username);
        System.out.println(url+"\n"+JSON.toJSONString(code));
        String r=HttpUtils.doPost2("http://"+path+""+method, JSON.toJSONString(code),null);
        Object parse = JSON.parse(r);
        return ((JSONObject)parse).getString("code");
    }
    public Map<String,String> get_token(String username,String password) {
        return  get_token2(username,password);
        //以下方法作废，
       /* String ip= PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String method=PropertiesUtils.loadProperties("openapi").getProperty("get_token");

        String url="http://"+ip+"/api";
        String code=this.get_code(username,password);
        String client_secret="c31b32364ce19ca8fcd150a417ecce58";
        String client_id="api";
        String redirect_uri="http://"+ip+"/oauth";
        method="http://"+ip+method+"?url="+url+"&code="+code+"&client_secret="+client_secret+"&client_id="+client_id+"&redirect_uri="+redirect_uri;
        String s = HttpUtils.doGet2(method,null,null);
        Object parse = JSON.parse(s);
        this.get_refresh_token(((JSONObject)parse).getString("refresh_token"));
        Map<String,String>map=new HashMap<>();
        map.put("access_token",((JSONObject)parse).getString("access_token"));
        map.put("refresh_token",((JSONObject)parse).getString("refresh_token"));
        return map;*/
    }
    public Map<String,String> get_token2(String username,String password) {
        Map<String,String> map=new HashMap<>();
        try {
            String ip= PropertiesUtils.loadProperties("openapi").getProperty("ip");
            String method=PropertiesUtils.loadProperties("openapi").getProperty("token");
            String url="http://"+ip+method;
            RequestTokenBody rtb=new RequestTokenBody();
            rtb.setUsername(username);
            rtb.setPassword(password);
            JSONObject parse=(JSONObject) JSONObject.parse(HttpUtils.doPost0(url,rtb.toString()));
            map.put("access_token",parse.getString("access_token"));
        }catch (Exception e){

        }

        /*grant_type:password
        client_id:api
        client_secret:c31b32364ce19ca8fcd150a417ecce58
        username:RKB003
        password:123456*/
        return map;
    }
    public String getTokenString(){
       String username=PropertiesUtils.loadProperties("openapi").getProperty("username");
        String password=PropertiesUtils.loadProperties("openapi").getProperty("password");
        return this.get_token(username,password).get("access_token");
    }
    public void get_refresh_token(String refresh_token){
       /* url："http://ip地址或域名/api"
        client_secret："c31b32364ce19ca8fcd150a417ecce58"
        client_id："api"
        refresh_token：在*/
        String ip= PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String method=PropertiesUtils.loadProperties("openapi").getProperty("get_token");
        String url="http://"+ip+"/api";
        String client_secret="c31b32364ce19ca8fcd150a417ecce58";
        String client_id="api";
        method="http://"+ip+method+"?url="+url+"&client_secret="+client_secret+"&client_id="+client_id+"&refresh_token="+refresh_token;
        HttpUtils.doGet2(method,null,null);

    }
    public static void main(String[] args) throws Exception{
        WebApiUtils wau=new WebApiUtils();
        //Map<String,String> map = wau.get_token("admin", "123456");
      //  wau.get_refresh_token(map.get("refresh_token"));
       // System.out.println(map.get("access_token"));
        RequestTokenBody rtb=new RequestTokenBody();
        rtb.setUsername("RKB006");
        rtb.setPassword("123456");
        System.out.println(JSON.toJSONString(rtb));
        String url="http://47.100.197.8/api/oauth/token";
        Object parse = JSON.parse( HttpUtils.doPost0(url,rtb.toString()));

        System.out.println();
    }
    class RequestCode {
        private String username;
        private String password;
        private String url;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
class RequestTokenBody{
   /* grant_type:password
    client_id:api
    client_secret:c31b32364ce19ca8fcd150a417ecce58
    username:RKB003
    password:123456*/
    private String grant_type="password";
    private String client_id="api";
    private String client_secret="c31b32364ce19ca8fcd150a417ecce58";
    private String username;
    private String password;

    public String getGrant_type() {
        return grant_type;
    }

    public void setGrant_type(String grant_type) {
        this.grant_type = grant_type;
    }

    public String getClient_id() {
        return client_id;
    }

    public void setClient_id(String client_id) {
        this.client_id = client_id;
    }

    public String getClient_secret() {
        return client_secret;
    }

    public void setClient_secret(String client_secret) {
        this.client_secret = client_secret;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "grant_type=password&client_id=api&client_secret=c31b32364ce19ca8fcd150a417ecce58&username="+username+"&password="+password;
    }
}